package com.plugin.mybatis.generator.lombok;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.util.Properties;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

/**
 * @author xiangshiming
 */
public class SimpleCommentGenerator extends DefaultCommentGenerator {
    private static final String APIMODELPROPERTYFLAG = "apiModelPropertyFlag";
    private static String apiModelPropertyFlag = "";
    /**
     * 设置用户配置参数
     */
    public void addConfigurationProperties(Properties properties) {
        // 调用父类方法
        super.addConfigurationProperties(properties);
        // 获取suppressAllComments参数值
        apiModelPropertyFlag = properties.getProperty(APIMODELPROPERTYFLAG);
    }

    public SimpleCommentGenerator() {
    }


    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (introspectedTable.getPrimaryKeyColumns().size() == 0) {
            throw new RuntimeException("表"+introspectedTable.getFullyQualifiedTable().getIntrospectedTableName()+"没有设置主键。请设置");
        } else {
            IntrospectedColumn pkColumn = (IntrospectedColumn)introspectedTable.getPrimaryKeyColumns().get(0);
            String remake = introspectedColumn.getRemarks().replace("\n", "").replace("\r", "").replace('"', '\'');
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * " + remake);
            field.addJavaDocLine(" * column:" + introspectedColumn.getActualColumnName());
            this.addJavadocTag(field, false);
            field.addJavaDocLine(" */");
            if( !(stringHasValue(apiModelPropertyFlag) && "false".equals(apiModelPropertyFlag)) ){
                field.addAnnotation("@ApiModelProperty(value = \"" + remake + "\")");
            }
            /*if (introspectedColumn.getActualColumnName().equals(pkColumn.getActualColumnName())) {
                field.addAnnotation("@Id");
            }*/
        }
    }
}
